package com.xm.thread.wait2notify;

/**
 * 问题：
 *   需要手动管理锁和条件
 *   易出现死锁或未唤醒（notify()可能丢失信号）
 *   难以扩展复杂场景（如多条件等待）
 */
public class WaitToNotify01 {
    private int data;
    private boolean available = false;

    public synchronized int get() {
        while (!available) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        available = false;
        notifyAll();
        return data;
    }

    public synchronized void put(int data) {
        while (available) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        this.data = data;
        available = true;
        notifyAll();
    }
}
